package com.xuantian.baoming.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuantian.baoming.entity.OrderInfo;
import com.xuantian.baoming.enums.OrderStatus;
import com.xuantian.baoming.mapper.OrderInfoMapper;
import com.xuantian.baoming.service.OrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

@Service
@Slf4j
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    @Override
    public void updateStatusByOrderNo(String orderNo, OrderStatus orderStatus) {
        log.info("更新订单状态 ===> {}", orderStatus.getType());

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setStatus(orderStatus.getType());

        orderInfoMapper.update(orderInfo, queryWrapper);
    }

    @Override
    public String getOrderStatus(String orderNo) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);
        OrderInfo orderInfo = orderInfoMapper.selectOne(queryWrapper);
        if(orderInfo == null){
            return null;
        }
        return orderInfo.getStatus();

    }

    /**
     * 查询创建超过minutes分钟并且未支付的订单
     * @param minutes
     * @return
     */
    @Override
    public List<OrderInfo> getNoPayOrderByDuration(int minutes) {

        Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", OrderStatus.NOTPAY.getType());
        queryWrapper.le("create_time", instant);

        List<OrderInfo> orderInfoList = orderInfoMapper.selectList(queryWrapper);

        return orderInfoList;
    }

    /**
     * 插入订单信息
     * @param orderInfo
     */
    @Override
    public OrderInfo createOrder(OrderInfo orderInfo) {

        //查询数据库中是否已经有了订单
        //没有就才入
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contact", orderInfo.getContact());
        queryWrapper.eq("name", orderInfo.getName());
        queryWrapper.eq("status", OrderStatus.NOTPAY.getType());
        OrderInfo orderInfo1 = orderInfoMapper.selectOne(queryWrapper);
        if(orderInfo1 != null){
            log.info("订单已存在");
            return orderInfo1;
        }
        orderInfoMapper.insert(orderInfo);
        return orderInfo;
    }

    @Override
    public OrderInfo selectOrderByContact(String contact, String name) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contact", contact);
        queryWrapper.eq("name", name);
        queryWrapper.eq("status", OrderStatus.SUCCESS.getType());
        OrderInfo orderInfo1 = orderInfoMapper.selectOne(queryWrapper);
        return orderInfo1;
    }
}
